# Create your views here.
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import Context, loader
from widpy.data.models import Tema, Pestanha

def cambiarTema(request, t_id):
	p = Tema.objects.get(id=t_id)
	user_id = request.user.id
	is_auth = request.user.is_authenticated()
	if(is_auth):
		estadoviejo = Pestanha.objects.get(usuario=user_id, estado=1)
		estadoviejo.tema_id = t_id
		estadoviejo.save()
	else:
		if "nro_pes" in request.COOKIES:
			pest_activa = request.COOKIES["pes_active"]
			#num_tem = request.COOKIES["t"+str(pest_activa)]
			response = HttpResponseRedirect('/inicio/')
			#cant_pes = request.COOKIES["nro_pes"]
			#cpes = string.atoi(cant_pes,10)
			response.delete_cookie("t"+str(pest_activa))
			response.delete_cookie("nt"+str(pest_activa))
			p = Tema.objects.get(id=t_id)
			response.set_cookie("t"+str(pest_activa), p.url, 365*24*60*60)
			response.set_cookie("nt"+str(pest_activa), t_id, 365*24*60*60)
			return response
	#return render_to_response('webpage/inicio.html', {'temaurl': p.url})
	return HttpResponseRedirect('/inicio/')
